package frontera;
import control.ClientesCTR;
import entidad.Clientes;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class ClientesGUI extends javax.swing.JPanel {
    public ClientesCTR clientesCTR;
    private String _action; //insertar o actualizar
    private int index_edit;

    public ClientesGUI() {
        clientesCTR = new ClientesCTR();
        initComponents();
        _init();
    }

    public void _init(){
        _action = "insertar";
        tabular();
    }

    public void tabular() {
        Iterator _iterator_clientes = clientesCTR.getClientes().iterator();
        Clientes _aux_cliente;
        //DefaultTableModel _data = (DefaultTableModel)(grid_tb.getModel());
        DefaultTableModel _data = new DefaultTableModel(new Object []{"Cedula","Nombre"},0);
        while(_iterator_clientes.hasNext()){
            _aux_cliente =  (Clientes)(_iterator_clientes.next());
            _data.addRow(new Object []{_aux_cliente.getCedula(),_aux_cliente.getNombre()}) ;
            //_data.setValueAt(_aux_cliente.getNombre() , _data.getRowCount()-1 ,1) ;
        }
        grid_tb.setModel(_data);
    }

    public void clear(){
        cedula_txt.setText("");
        nombre_txt.setText("");
        _action = "insertar";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        guardar = new javax.swing.JButton();
        nuevo = new javax.swing.JButton();
        eliminar = new javax.swing.JButton();
        cedula_txt = new javax.swing.JTextField();
        nombre_txt = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        grid_tb = new javax.swing.JTable();

        jLabel1.setText("Modulo de Clientes:");

        jLabel2.setText("Nombre");

        jLabel3.setText("Cedula");

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardar_cliente(evt);
            }
        });

        nuevo.setText("Nuevo");
        nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevo(evt);
            }
        });

        eliminar.setText("Eliminar");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminar(evt);
            }
        });

        grid_tb.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cedula", "Nombre"
            }
        ));
        grid_tb.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editar(evt);
            }
        });
        jScrollPane1.setViewportView(grid_tb);
        grid_tb.getColumnModel().getColumn(0).setPreferredWidth(100);
        grid_tb.getColumnModel().getColumn(0).setMaxWidth(100);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(14, 14, 14)
                                .addComponent(nombre_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(cedula_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(272, Short.MAX_VALUE)
                        .addComponent(guardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cedula_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(nombre_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardar)
                    .addComponent(nuevo)
                    .addComponent(eliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    public void show_msn(String _msn){
        JOptionPane _dialog = new JOptionPane(_msn);
        JOptionPane.showMessageDialog(this, _msn);
    }
   
    private void guardar_cliente(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardar_cliente
       // TODO add your handling code here:
        Clientes _cliente = new Clientes();


            try{
                _cliente.setCedula( Integer.parseInt(cedula_txt.getText()) );
            }
            catch(Exception _exception){
                _cliente.setCedula(0);
            }

            _cliente.setNombre(nombre_txt.getText() );
        if (_action == "insertar"){
            clientesCTR.add_cliente(_cliente);
        }

        if (_action == "editar"){
            clientesCTR.getClientes().get(index_edit).setCedula(_cliente.getCedula() ) ;
            clientesCTR.getClientes().get(index_edit).setNombre(_cliente.getNombre() ) ;
        }


        tabular();
        clear();
        show_msn( ClientesCTR.insert_ok );
    }//GEN-LAST:event_guardar_cliente

    private void eliminar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminar
         // TODO add your handling code here:
        try{
            if ( cedula_txt.getText() != "" ){
                clientesCTR.delete_cliente( Integer.parseInt(cedula_txt.getText()) );
                tabular();
                clear();
            }
        }catch(Exception e){

        }
    }//GEN-LAST:event_eliminar

    private void editar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editar
        // TODO add your handling code here:
        _action = "editar";

        //cliente_edit = clientesCTR.getClientes().get
        int fila = grid_tb.rowAtPoint(evt.getPoint());
        int columna = grid_tb.columnAtPoint(evt.getPoint());
        if (fila > -1) {
            cedula_txt.setText(  grid_tb.getModel().getValueAt(fila, 0).toString() );
            nombre_txt.setText(  grid_tb.getModel().getValueAt(fila, 1).toString() );

            index_edit = clientesCTR.search_cliente_by_index( Integer.parseInt(cedula_txt.getText())  );
        }
    }//GEN-LAST:event_editar

    private void nuevo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevo
        clear();
    }//GEN-LAST:event_nuevo


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cedula_txt;
    private javax.swing.JButton eliminar;
    private javax.swing.JTable grid_tb;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nombre_txt;
    private javax.swing.JButton nuevo;
    // End of variables declaration//GEN-END:variables

}
